package org.dreamwork.misc;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-7-8
 * Time: 13:51:34
 */
public class AlgorithmUtil {
    public static byte[] md5 (byte[] data) throws NoSuchAlgorithmException {
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(data);
        return mdTemp.digest ();
    }

    public static String md5 (String data) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        byte[] ret = md5 (data.getBytes ("iso-8859-1"));
        StringBuilder builder = new StringBuilder ();
        for (byte i : ret) builder.append (String.format ("%02x", i));
        return builder.toString ();
    }
}